package com.whut.action;

import java.util.List;

import net.sf.json.JSONObject;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.whut.model.User;
import com.whut.service.UserManager;
import com.whut.vo.UserRegisterInfo;


public class ValidateNameAction extends ActionSupport implements ModelDriven  {
	
	private UserManager um;	
	private List<User> users;	
	private User user;   	
	private String username;	
	private String result;
	
	User u = new User();	
	private UserRegisterInfo userInfo = new UserRegisterInfo();
	
	public ValidateNameAction(){
		System.out.println("userName check!");
	} 
	 
	
	@Override
	public String execute(){			
		
		System.out.println(userInfo.getUsername());		
	
		u.setUsername(userInfo.getUsername());		
		JSONObject jsonObj = new JSONObject();
		jsonObj.put("username",u.getUsername());		
		 
		try {			 
			if(um.exists(u)) {				
				jsonObj.put("validate",true);
				jsonObj.put("msg","用户名" + u.getUsername() + "已注册");				
		    } else {
		    	jsonObj.put("validate",false);
				jsonObj.put("msg","用户名" + u.getUsername() + "可用");					
		    }
			// 调用json对象的toString方法转换为字符串然后赋值给result 
			result = jsonObj.toString();
			System.out.println(result);  //测试result
			return SUCCESS;
			 
		} catch (Exception e) {
			// TODO Auto-generated catch block
			jsonObj.put("msg","异常");
			result = jsonObj.toString();
			System.out.println(result);  //测试result
			System.out.println(e);
			return SUCCESS;
		}				 
	}
	
	public String ValidateEamil() {
		User u = new User();
		
		System.out.println(userInfo.getEmail());		
		u.setUsername(userInfo.getUsername());	
		u.setEmail(userInfo.getEmail());
		JSONObject jsonObj = new JSONObject();
		jsonObj.put("username",u.getUsername());		
		 
		try {					
			// 调用json对象的toString方法转换为字符串然后赋值给result 
			result = jsonObj.toString();
			System.out.println(result);  //测试result
			return SUCCESS;
			 
		} catch (Exception e) {
			// TODO Auto-generated catch block
			jsonObj.put("msg","异常");
			result = jsonObj.toString();
			System.out.println(result);  //测试result
			System.out.println(e);
			return SUCCESS;
		}				
	}
		 
	
	//@Override
	public Object getModel() {
		return userInfo;
	}
	
	 
	
	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

 
	
	public List<User> getUsers() {
		return users;
	}

	public void setUsers(List<User> users) {
		this.users = users;
	}
	 
	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getResult() {
		return result;
	}


	public void setResult(String result) {
		this.result = result;
	}


	public UserRegisterInfo getUserInfo() {
		return userInfo;
	}


	public void setUserInfo(UserRegisterInfo userInfo) {
		this.userInfo = userInfo;
	}


	public UserManager getUm() {
		return um;
	}


	public void setUm(UserManager um) {
		this.um = um;
	}


	public User getU() {
		return u;
	}


	public void setU(User u) {
		this.u = u;
	}
	
}
